<?php 
/**
* 上传类
*/
class Upload
{
	public $config=array(
		'path'=>"./Upload",
		'exts'=>["jpg","png","gif"],
		'maxsize'=>100000,
		);//配置
	public $data;//原始数据
	public $error;//信息

	public $errorinfo = array(
		1=>"上传的文件超过了 PHP.ini 中 upload_max_filesize 选项限制的值",
		2=>"上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值",
		3=>"文件只有部分被上传",
		4=>"没有文件被上传",
		6=>"找不到临时文件夹。php 4.3.10 和 PHP 5.0.3 引进",
		7=>"文件写入失败。PHP 5.1.0 引进"
		);//上传错误代码对应的错误原因
	/**
	 * [__construct 构造函数]
	 * @Author   Xuebingsi
	 * @DateTime 2017-07-15T09:36:34+0800
	 * @param    array                    $config [description]
	 */
	public function __construct($config=array())
	{	
		//合并配置
		$this->config = array_merge($this->config,$config);
	}
	/**
	 * [save 保存方法]
	 * @Author   Xuebingsi
	 * @DateTime 2017-07-15T09:36:58+0800
	 * @return   [type]                   [description]
	 */
	public function save()
	{
		//获取原始上传数据
		$this->data = current($_FILES);
		$this->isError();
		$this->checkType();
		$this->checkSize();
		$this->movefile();
		return $this->error;
	}
	/**
	 * [isError 检测错误代码]
	 * @Author   Xuebingsi
	 * @DateTime 2017-07-15T09:37:27+0800
	 * @return   boolean                  [description]
	 */
	public function isError()
	{
		foreach ($this->data['error'] as $k => $v) {
			if($v!=0){
				//有错误的可以错误信息保存起来
				$this->error[$k]=$this->errorinfo[$v];
			}
		}
	}
	/**
	 * [checkType 检测类型]
	 * @Author   Xuebingsi
	 * @DateTime 2017-07-15T09:38:04+0800
	 * @return   [type]                   [description]
	 */
	public function checkType()
	{
		foreach ($this->data['name'] as $k => $v) {
			// 有错误存在的不检测
			if(!isset($this->error[$k])){
				//获取后缀
				$arr = explode('.', $v);
				$ext = end($arr);
				// 判断后缀
				if(!in_array($ext, $this->config['exts'])){
					$this->error[$k]=$v."文件类型不符合要求";
				}
			}
		}
	}

	/**
	 * [checkSize 检测大小]
	 * @Author   Xuebingsi
	 * @DateTime 2017-07-15T09:38:51+0800
	 * @return   [type]                   [description]
	 */
	public function checkSize()
	{
		foreach ($this->data['size'] as $k => $v) {
			// 有错误存在的不检测
			if(!isset($this->error[$k])){
				// 判断大小
				if($v>$this->config['maxsize']){
					$this->error[$k]=$this->data['name'][$k]."文件过大，限制是".$this->config['maxsize']."字节";
				}
			}
		}
	}
	/**
	 * [movefile 移动文件]
	 * @Author   Xuebingsi
	 * @DateTime 2017-07-15T09:39:16+0800
	 * @return   [type]                   [description]
	 */
	public function movefile()
	{
		foreach ($this->data['tmp_name'] as $k => $v) {
			// 有错误存在的不检测
			if(!isset($this->error[$k])){

				$path = $this->config['path'];

				$name = uniqid();
				//判断生成目录
				is_dir($path) || mkdir($path);
				//生成当前日期
				$date = date("Y-m-d");
				//判断生成当前日期目录
				is_dir($path."/".$date) || mkdir($path."/".$date);
				//取后缀
				$arr = explode('.', $this->data['name'][$k]);
				$ext = end($arr);

				//组移动目标路径
				$dest = $path."/".$date."/".$name.".".$ext;
				//移动文件
				$res = move_uploaded_file($v, $dest);

				if($res){
					$this->error[$k]=$dest;
				}else{
					$this->error[$k]="文件移动失败";
				}
			}
		}
	}
}
 ?>